/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package be.contribute.pihome.ws;

import be.contribute.common.pojo.Temperature;
import be.contribute.pihome.DBController;
import be.contribute.pihome.devices.DeviceController;
import java.text.SimpleDateFormat;
import java.util.List;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;

/**
 *
 * @author debian
 */
@Path("/")
public class HomeService {
    
    public static final String MESSAGE = "Hello World from JAX-RS!";
    DBController db = DBController.getInstance();
    DeviceController device = new DeviceController();

    @GET
    @Produces({MediaType.TEXT_PLAIN, MediaType.TEXT_HTML})
    public String getText() {
        return "This is the Temperature Monitor Service from Contribute";
    }
        
    @GET
    @Path("count")
    @Produces(MediaType.TEXT_PLAIN)
    public String getCount() {
        return String.valueOf(db.getTemperatureFromRoom(0).size());
    }

    @GET
    @Path("list")
    @Produces({MediaType.APPLICATION_XML,
               MediaType.APPLICATION_JSON})
    public List<Temperature> getList(@QueryParam("id") int pRoomId) {
        return db.getTemperatureFromRoom(pRoomId);
    }
    
    @GET
    @Path("room")
    @Produces({MediaType.APPLICATION_XML,
               MediaType.APPLICATION_JSON})
    public Temperature getCurrentTemperature(@QueryParam("id") int pRoomId) {
        return db.getCurrentTemperatureFromRoom(pRoomId);
    }
    
    @GET
    @Path("roomHTML")
    @Produces({MediaType.TEXT_HTML})
    public String getCurrentTemperatureHTML(@QueryParam("id") int pRoomId) {
            Temperature vTemp = db.getCurrentTemperatureFromRoom(pRoomId);
            if(vTemp != null)
            {
            SimpleDateFormat formatter = new SimpleDateFormat("HH:mm dd-MM-yyyy");
            return "<html><body>"
                    + "Room: "+db.getRoomName(vTemp.getRoomId()) +" <br/>"
                    + "Temperature: "+vTemp.getTempValue()+" °C <br/>"
                    + "Humidity: "+vTemp.getHumValue()+" % <br/>"
                    + "Time: "+formatter.format(vTemp.getTime())
                    +" <br/>"
                + "</body></html>";
            }                
        return "No temperature found.";
    }
    
    @POST
    @Path("tempInput")
    @Consumes(MediaType.APPLICATION_JSON)
    public void consumeInput(Temperature pInput)
    {
        pInput.print();
        db.insertIntoTemperature(pInput);
    }
    
    @GET
    @Path("airco")
    @Produces(MediaType.TEXT_HTML)
    public String aircoControl(@QueryParam("id") int pRoomId, @QueryParam("state") int pOn)
    {
        if(pRoomId == 1)
        {
            if(pOn == 1)
            {
                device.turnAircoOn();
                return "Airco on.";
            }
            else
                device.turnAircoOff();
        
        return "Airco off.";
        }
        else
           return "There is no airco in this room.";       
    }
    
    @GET
    @Path("heater")
    @Produces(MediaType.TEXT_HTML)
    public String heatControl(@QueryParam("id") int pRoomId, @QueryParam("state") int pOn)
    {
        if(pRoomId == 1)
        {
            if(pOn == 1)
            {
                device.turnOnHeater();
                return "Heater is on.";
            }
            else
                device.turnOffHeater();
        
        return "Heater is off.";
        }
        else
           return "There is no heater in this room.";       
    }
    
    @GET
    @Path("cam")
    @Produces(MediaType.TEXT_HTML)
    public String cameraControl(@QueryParam("type") String pType, @QueryParam("name") String pName)
    {
        System.out.println(pType + " - " +pName);
        if(pType.toLowerCase().equals("jpg"))
        {
            if(pName.length() == 0)
                pName = "default";
            device.takePicture(pName);
            return "<html><body>"
                    + "<img src=\"http://192.168.1.101/"+pName+".jpg\">"
                    + "http://192.168.1.101/"+pName+".jpg"
                    + "</body></html>";
        }
        if(pType.toLowerCase().equals("h264"))
        {
            if(pName.length() == 0)
                pName = "default";
            device.takePicture(pName);
            return "Filmed for 5sec.";
        }
        return "No action taken.";
        
    }
    
}
